import wx
import random

class MyFrame(wx.Frame):
    name_list = ['Jack', 'Tom', 'Alice', 'Bob']  # 类属性
    def __init__(self):
        super().__init__(None, title = '随机点名', size = (400, 400))
        # 面板
        panel = wx.Panel(self)
        self.text = wx.StaticText(panel, label = '张三',
                             size = (200, 80), pos = (100, 50),
                             style = wx.ALIGN_CENTER)
        # 文本，设置背景颜色，字体颜色，字体类型风格
        # 字体大小、字体家族、倾斜程度、加粗程度
        font = wx.Font(48,
                       wx.FONTFAMILY_SWISS,
                       wx.FONTSTYLE_NORMAL,
                       wx.FONTWEIGHT_BOLD)
        self.text.SetFont(font)
        # 前景色：字体颜色
        self.text.SetForegroundColour(wx.BLACK)
        # 背景色
        self.text.SetBackgroundColour(wx.Colour('#0000FF'))
        # 按钮
        start_btn = wx.Button(panel, label = '开始',
                              size = (100, 50), pos = (80, 300))
        end_btn = wx.Button(panel, label = '结束',
                            size = (100, 50), pos = (240, 300))
        for btn in [start_btn, end_btn]:
            btn.SetForegroundColour(wx.BLACK)
            btn.SetBackgroundColour(wx.Colour('#CCCCCC'))
            btn.SetFont(wx.Font(16, wx.FONTFAMILY_DEFAULT,
                                wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        # 定义定时器
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.change_text, self.timer)
        start_btn.Bind(wx.EVT_BUTTON, self.start)
        end_btn.Bind(wx.EVT_BUTTON, self.end)
        self.Show()

    # 随机更新名字和字体颜色
    def change_text(self, event):
        # print('更新文本')
        self.text.SetLabelText(random.choice(self.name_list))
        # rgb(0, 0, 0)
        self.text.SetForegroundColour(wx.Colour(
            random.randint(0, 255),
            random.randint(0, 255),
            random.randint(0, 255)
        ))

    def start(self, event):
        # print('开始随机')
        # 设置周期间隔，并开始运行
        self.timer.Start(200)  # 每200毫秒执行一次

    def end(self, event):
        # print('停止随机')
        self.timer.Stop()

# 程序入口
if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()